home *** CD-ROM | disk | FTP | other *** search
- Path: guillotine.mtl.dmr.ca!news
- From: Bernard Drolet <Bernard.Drolet@dmr.ca>
- Newsgroups: comp.lang.c++
- Subject: Typecasting
- Date: Fri, 12 Jan 1996 23:48:00 -0800
- Organization: Groupe DMR
- Message-ID: <30F763B0.319@dmr.ca>
- NNTP-Posting-Host: slip29.mtl.dmr.ca
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b3 (Win16; I)
-
- I'm writing a program using the Doc-View method.
-
- Presently, the main classes are (simplified)
-
- class Base
- {
- int GetType() = 0;
- // some other methods and variables
- };
-
- class A : public Base
- {
- int GetType() { return TypeA; }
- // methods and variables
- };
-
- class B : public Base
- {
- int GetType() { return TypeB; }
- // methods and variables
- };
-
- class Doc
- {
- List<Base> ListOfBaseObjects;
- Doc();
- };
-
- class View
- {
- Doc CurrentDocument;
- void Paint();
- void PaintAObject();
- void PaintBObject();
- };
-
- void Doc::Doc()
- {
- A* a = new A();
- B* b = new B();
-
- ListOfBaseObjects.Add(a);
- ListOfBaseObjects.Add(b);
- }
-
- void View::Paint()
- {
- Iterator<CurrentDocument.ListOfBaseObjects> ObjectsIterator;
-
- while ( ObjectsIterator )
- {
- switch ( ObjectsIterator.Current()->GetType() )
- {
- case TypeA:
- PaintAObject();
- break;
- case TypeB:
- PaintBObject();
- break;
- }
- ObjectsIterator++;
- }
- }
-
-
- What I would like better is to remove the switch statement in the paint() function and
- find a way to define functions looking like Paint(A& a), Paint(B& b) and to call them
- directly, but is there a way to discover what type an object that was inserted in the list
- is?
-
- I would like to replace the switch in the paint() with a statement like
- while ( ObjectsIterator )
- {
- Paint(ObjectsIterator.Current())
- ...
-
- So can I typecast the abstract Base object contained in the list to its actual type ?
-
-
-
- ==========================================================
- Bernard Drolet Bernard.Drolet@dmr.ca
- Groupe DMR
- 1200 McGill College
- Montreal, Quebec, Canada
- ==========================================================
-